﻿// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill'
import App from './App'
import router from './router'
import store from './store'
import FastClick from 'fastclick'
import VueScroller from 'vue-scroller'
import VueTouch from 'vue-touch'
import 'assets/sass/style.scss'
import { RouterExtends } from 'base'
import { verifyToken, verifyVIP, wxLogin, verifyBaseInfo } from 'api/account'

Vue.use(MINT.Lazyload, {
  loading: require('assets/image/lazyload-loading.png'),
  error: require('assets/image/placeholder_mini.png')
})

Vue.use(VueScroller)
Vue.use(VueTouch, {
  name: 'v-touch'
})

VueTouch.config.swipe = {
  threshold: 100 // 手指左右滑动距离
}

FastClick.attach(document.body)

Vue.config.productionTip = false

Vue.prototype.routeName = ''

Vue.mixin({
  data() {
    return {
      loaded: 0
    }
  },
  created() {
    this.$route && (this.routeName = this.$route.name)
  },
  activated() {
    if (this.loaded) {
      this.loaded++
    }
  },
  watch: {
    loaded(val) {
      if (val) {
        Indicator.close()
      }
    }
  }
})

RouterExtends.init(router)

router.afterEach(async (to, from) => {
  let meta = to.meta
  // 路由守卫处理
})

router.beforeEach(async (to, from, next) => {
  let meta = to.meta
  if (!meta) {
    to.meta = {}
  }
  if (!verifyToken()) {
    // return wxLogin()
  }

  if (!(to.name && from.name && from.name.indexOf(to.name) === 0)) {
    if (meta.showWaiting) {
      Indicator.open()
    }
  }
  next()
})

function initApp() {
  /* eslint-disable no-unused-vars */
  window.vm = new Vue({
    el: '#app',
    data: {},
    router,
    store,
    components: {
      App
    },
    template: '<App/>',
    beforeCreate() {},
    mounted() {},
    methods: {},
    watch: {
      $route(to, from) {
        let fromView = from.name || from.path
        let toView = to.name || to.path
        console.log(`trackView:${fromView} to ${toView}`)
      }
    }
  })
}

initApp()
